﻿Public Class ProjectListForm


    Private WithEvents _projectList As New Hashtable()
    Public Update_Flag As Boolean = False

    '终端被选择事件
    '  Public Event ProjectSelected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)


    Public ReadOnly Property ProjectList() As Hashtable
        Get
            Return _projectList
        End Get
    End Property


    Private Sub ProjectListForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FillDataGridViewColumn()

        LoadProjects()

    End Sub


    Public Sub LoadProjects()

        Me.dgvProjectList.Rows.Clear()
        _projectList.Clear()

        Dim il As IList(Of ProjectInfo.CarProjectInfo)
        il = CarProjectService.GetAllCarProjectInfo()

        'For i As Integer = 0 To 9
        '    Dim pi As New ProjectItem()
        '    pi.ProjectName = Now.AddDays(i)
        '    pi.ProjectMacID = Now.AddDays(i)
        '    il.Add(pi)
        'Next

        If il IsNot Nothing Then
            Dim pi As ProjectInfo.CarProjectInfo
            For i As Integer = 0 To il.Count - 1
                pi = il(i)
                Dim row As DataGridViewRow = GetNewRow()
                row.Cells(0).Value = i + 1
                If pi.CarProjectType > 0 Then
                    row.Cells(1).Value = ProjectTypeList(pi.CarProjectType - 1)
                Else
                    row.Cells(1).Value = Nothing
                End If
                row.Cells(2).Value = pi.CarProjectName
                'If pi.LocalRunStatus > 0 Then
                '    row.Cells(3).Value = RunStatusList(pi.LocalRunStatus - 1)
                'Else
                row.Cells(3).Value = Nothing
                'End If
                ' row.Cells(9).Value = pi.Remark
                row.Tag = pi
                Me.dgvProjectList.Rows.Add(row)
                '_projectList.Add(pi.ProjectMacID, row)
            Next
        End If

    End Sub


#Region " DataGridView操作 "

    '创建新行
    Private Function GetNewRow() As DataGridViewRow
        Dim row As New DataGridViewRow()
        Dim cell As DataGridViewCell

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        cell = New DataGridViewTextBoxCell()
        row.Cells.Add(cell)

        Return row
    End Function

    '
    Private Sub dgvProjectList_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvProjectList.RowEnter

        '  RaiseEvent ProjectSelected(sender, e)

    End Sub

#End Region

    '[修改终端]
    Private Sub tsmiEditPrj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiEditPrj.Click, dgvProjectList.DoubleClick
        'Dim pi As ProjectItem
        'pi = GetSelectedItem()
        'If pi Is Nothing Then
        '    'msg.Show("请选择要修改的终端。")
        '    Exit Sub
        'End If

        'Dim frm As New ProjectBasicInfoForm(pi)
        'If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
        '    Update_Flag = True '参数发生修改
        '    LoadProjects()
        'End If
    End Sub

    '[删除终端]
    Private Sub tsmiDelPrj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiDelPrj.Click
        'Dim pi As ProjectItem
        'pi = GetSelectedItem()
        'If pi Is Nothing Then
        '    'msg.Show("请选择要删除的终端。")
        '    Exit Sub
        'End If

        'Dim tip As String
        'tip = String.Format("是否要删除终端【{0}】的所有配置信息和历史数据？", pi.ProjectName)
        'If msg.ShowYesNo(tip) = Windows.Forms.DialogResult.Yes Then
        '    If ProjectService.Delete(pi, ProjectService.DevTypeMaxNetid(pi.ProjectID)) = True Then
        '        Update_Flag = True '参数发生修改
        '        Me.LoadProjects()
        '        msg.Show("删除成功。")
        '    Else
        '        msg.ShowError("删除失败。")
        '    End If
        'End If
    End Sub

    '[启停纪录]
    Private Sub tsmiStartup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiStartup.Click
        'Dim pi As ProjectItem
        'pi = GetSelectedItem()
        'If pi Is Nothing Then
        '    Exit Sub
        'End If

        'Dim frm As New StartupViewForm(pi, False)
        'frm.Show(Me)
    End Sub

    '[历史数据]
    Private Sub tsmiPrjHisData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiPrjHisData.Click
        'Dim pi As ProjectItem
        'pi = GetSelectedItem()
        'If pi Is Nothing Then
        '    Exit Sub
        'End If

        'Dim frm As New HDViewForm(pi)
        'frm.Show(Me)
    End Sub

    Private Function GetSelectedItem() As ProjectInfo.CarProjectInfo
        If Me.dgvProjectList.CurrentRow Is Nothing Then
            Return Nothing
        End If

        Dim pi As ProjectInfo.CarProjectInfo
        pi = CType(Me.dgvProjectList.CurrentRow.Tag, ProjectInfo.CarProjectInfo)
        Return pi

    End Function

    Private Sub FillDataGridViewColumn()
        Me.dgvProjectList.Columns(9).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        Me.dgvProjectList.Columns(9).AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
    End Sub

    Private Sub ProjectListForm_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        FillDataGridViewColumn()
    End Sub

    Private Sub mmnuMailCfg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmnuMailCfg.Click
        Dim pi As ProjectInfo.CarProjectInfo
        pi = GetSelectedItem()
        If pi Is Nothing Then
            MessageBox.Show("请选择要配置的工程")
            Exit Sub
        End If

        Dim mailcfg As New CarMailCfg(pi.ProId, pi.CarProjectName)
        mailcfg.ShowDialog(Me)
    End Sub
End Class